package com.xiaoq.social.endpoint;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xiaoq.apis.content.ContentInfo4Vo;
import com.xiaoq.apis.content.IContentApi;
import com.xiaoq.apis.user.IUserApi;
import com.xiaoq.apis.user.UserInfo4SocialVo;
import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.social.entity.LikeitEntity;
import com.xiaoq.social.service.ILikeitService;
import com.xiaoq.users.entity.UserEntity;
import com.xiaoq.users.entity.UserEntity.RegMethod;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@RestController
@RequestMapping("/api/v1/likeit")
public class LikeitEndPoint {
	
	@Autowired
	private ILikeitService likeitSer;
	
	@Reference(interfaceClass=IContentApi.class, check=false, version="1.0.0")
	private IContentApi contentDubboApi;
	
	@Reference(interfaceClass=IUserApi.class, check=false, version="1.0.0")
	private IUserApi userDubboApi;
	
	private class UserLikeitStatus extends GenericDataResponse<Boolean>{}
	
	private class TgtLikeitNums extends GenericDataResponse<Long>{}
	
	private static final Logger LOG = LoggerFactory.getLogger(LikeitEndPoint.class);
	
	@ApiOperation(value = "点赞", notes = "点赞")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "点赞") })
	@PostMapping(value = "/user/{userId}", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> addLikeit(@PathVariable(value = "userId", required = false)@ApiParam(value="userId",required=false)String userId,
			@RequestParam(value = "tgtId", required = true) String tgtId,
			@RequestParam(value = "tgtType", required = true) String tgtType) {
		// 目前仅实现对内容的点赞
		boolean result = executeLikeitMed(userId, tgtId, tgtType);
		if(result){
			return GenericResponse.ok();
		}else{
			return GenericResponse.ng("点赞失败！");
		}
	}
	
	
	@ApiOperation(value = "取消点赞", notes = "取消点赞")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "取消点赞") })
	@DeleteMapping(value = "/user/{userId}", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> cancelLikeit(@PathVariable(value = "userId", required = false) @ApiParam(value="userId",required=false)String userId,
			@RequestParam(value = "tgtId", required = true) String tgtId) {
		boolean result = likeitSer.delLikeit(userId, tgtId);
		if(result){
			return GenericResponse.ok();
		}else{
			return GenericResponse.ng("取消点赞失败！");
		}
	}
	
	
	@ApiOperation(value = "根据目标Id获取点赞数量", notes = "根据目标Id获取点赞数量")
	@ApiResponses({ @ApiResponse(code = 200, response = TgtLikeitNums.class, message = "根据目标Id获取点赞数量") })
	@GetMapping(value = "/tgt/{tgtId}/count", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> GetLikeits(@PathVariable(value = "tgtId", required = true) String tgtId) {
		long likeitNums = likeitSer.getTgtNums(tgtId);
		return TgtLikeitNums.okWithData(likeitNums);
	}
	
	@ApiOperation(value = "是否已经点赞", notes = "是否已经点赞")
	@ApiResponses({ @ApiResponse(code = 200, response =  UserLikeitStatus.class, message = "是否已经点赞") })
	@GetMapping(value = "/user/{userId}/has", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> isFollow(@PathVariable(value = "userId", required = false) @ApiParam(value="userId",required=false)String userId,
			@RequestParam(value = "tgtId", required = true) String tgtId){
		
		boolean result = likeitSer.isLikeit(userId, tgtId);
		if(result){
			return UserLikeitStatus.okWithData(true);
			
		}else{
			return UserLikeitStatus.okWithData(false);
		}
	}
	
	private boolean executeLikeitMed(String userId,String tgtId,String tgtType){
		// 现在只能给文章点赞
		
		UserInfo4SocialVo userInfo = userDubboApi.getUserInfo4Social(userId);
		userId = userInfo.getId();
		String userLogo = userInfo.getLogoUrl(); 
		String userNickName = userInfo.getNickName();
		if(userId!=null){
			
			switch (tgtType) {
			case "30":	
				// 这里tgtId就是contentId
				ContentInfo4Vo contentInfo = contentDubboApi.getContentInfo4Vo(tgtId);
				
				if(contentInfo!=null){
					LikeitEntity likeit = new LikeitEntity();
					 
					
					
					String targetId  = contentInfo.getId();   
					String targetInfo = contentInfo.getIntroduction(); 
					String targetName = contentInfo.getName(); 
					String targetLogo = contentInfo.getImgUrl(); 
					String merchantId = contentInfo.getMerchantId(); 
					
					likeit.setUserId(userId);
					likeit.setUserLogo(userLogo);
					likeit.setUserNickName(userNickName);
					likeit.setTargetId(targetId);
					likeit.setTargetName(targetName);
					likeit.setTargetInfo(targetInfo);
					likeit.setTargetType(tgtType);
					likeit.setTargetLogo(targetLogo);
					likeit.setMerchantId(merchantId);
					
					boolean result = likeitSer.addLikeit(likeit);
					return result;
					
				}
				break;

			default:
				break;
			}
			
		}
				
		return false;
	}
	
	
	

}
